﻿using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAB50RVREP 
    {  
        public TAB50RVREP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            InitializeComponent();
            if (app.User.Trim() == "ITXV")
            {
                KRS = "B5";
                LFD = "000020";
                POS = "010";
            }

        }  

        public override void LOADBYAUF()
        {
            base.LOADBYAUF();
            if (MV == null)
                return;
            KRS = ORDERNUM.Substring(0, 2);
            LFD = ORDERNUM.Substring(2, 6);
            POS = ORDERNUM.Substring(8, 3);
            PM["ORDERNUM"] = ORDERNUM;
            TMV.LOAD(PM);
            vwMaster.View.MoveCurrentToFirst();

        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            base.QueryCommand(sender, e);
            ORDERNUM = KRS + LFD + POS;
        }

        private void btnRepaire_Click(object sender, RoutedEventArgs e)
        {
            if (dwAB == null)
                return;
            if (SQTY < 0)
            {
                cQTY.Visibility = Visibility.Visible;
                MessageBox.Show("请输入会修数量，然后继续点击我");
                return;
            }
            DataTable TBGD = cGD.TBMain.Copy();
            var gapos = (from DataRow row in TBGD.AsEnumerable()
                         select row["GAPOS"].ToString())
                       .Max();
            int pos = Convert.ToInt16((string)gapos);
            DataRow _dwGD = cGD.TBMain.NewRow();
            VGAAUF.Instance.SetUpNewRow(ref _dwGD);
            _dwGD["GAAUFKRS"] = dwAB["ABPAUFKRS"];
            _dwGD["GAAUFLFD"] = dwAB["ABPAUFLFD"];
            _dwGD["GAAUFPOS"] = dwAB["ABPAUFPOS"];
            _dwGD["GAPOS"] = Helpers.Helper.Right("00" + (pos + 1), 2);
            _dwGD["GAART"] = dwAB["ABPART"];
            _dwGD["GAVAR"] = dwAB["ABPVAR"];
            _dwGD["GAFARBE"] = dwAB["ABPFARBE"];
            _dwGD["GAKG1"] = SQTY;
            _dwGD["GAKG2"] = 0;
            _dwGD["GAKG3"] = 0;
            _dwGD["GAKG4"] = 0;
            _dwGD["GAKG5"] = 0;
            _dwGD["GAKG6"] = 0;
            _dwGD["GAKG7"] = 0;
            _dwGD["GANEUBEN"] = app.User;
            _dwGD["GANEUDAT"] = MV.date;
            VGAAUF.Instance.DeleteGAAUF(_dwGD, 1);
            VGAAUF.Instance.InsertGAAUF(_dwGD,app.company);//插入AB15
            cGD.TBMain.Rows.InsertAt(_dwGD, 0);
            PM["STARTD"] = TMV.STARTD;
            PM["YARNTYP"] = TMV.P1;
            PM["GSFACH"] = TMV.P2;
            PM["CART"] = dwAB["ABPART"].ToString() + dwAB["ABPVAR"].ToString() + dwAB["ABPFARBE"].ToString();
            PM["VKAUF"] = TMV.P4;
            PM["VKPAUF"] = TMV.P5;
            PM["QTYEND"] = SQTY * 0.01;
            PM["QTYMAT"] = SQTY * 0.01;
            PM["RUNID"] = this.RUNID; 

            PM["POS"] = Helpers.Helper.Left("9" + _dwGD["GAPOS"].ToString(), 3); 
            //ABPSOLLMENGE7保存了原始订单量
            PM["VKPQTY"] =Helpers.Helper.RoundX(Convert.ToDouble(dwAB["ABPSOLLMENGE7"].ToString()) * SQTY*0.01 / Convert.ToDouble(dwAB["ABPSOLLMENGE5"].ToString()),1);
            //以前不需要这个，现在规则统一了。
            VGAAUF.Instance.RVABPNEW(_dwGD, PM);//类似于以前的FP，现在是直接创建。不是没有差异吗
        }

    }
}
